/**   
 * @Title: SynchronizedExample1.java 
 * @Package com.liumm.concurrently.example.sync 
 * @Description: TODO
 * @author liumm   
 * @date 2018年4月18日 下午11:23:54 
 * @version V1.0   
 */
package com.liumm.concurrently.example.sync;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import lombok.extern.slf4j.Slf4j;

/** 
 * @ClassName: SynchronizedExample1 
 * @Description: TODO
 * @author liumm 
 * @date 2018年4月18日 下午11:23:54 
 *  
 */
@Slf4j
public class SynchronizedExample1 {

	public void test1(int j) {
		synchronized (this) {
			for (int i = 1; i <= 10; i++) {
				log.info("test1 {} - {}", j, i);
			}
		}
	}

	public synchronized void test2(int j) {
		for (int i = 1; i <= 10; i++) {
			log.info("test2 {} - {}", j, i);
		}
	}

	public static void main(String[] args) {

		SynchronizedExample1 example1 = new SynchronizedExample1();
		ExecutorService threadPool = Executors.newCachedThreadPool();
		threadPool.submit(() -> {
			example1.test1(1);
		});
		threadPool.submit(() -> {
			example1.test1(2);
		});
		threadPool.submit(() -> {
			example1.test1(3);
		});
		threadPool.submit(() -> {
			example1.test1(4);
		});
		threadPool.shutdown();
	}

}
